home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Textfiles / zines / Midnight-Raid / midnight raid 8_app_640x480.sit / tmr 8 small.rsrc / TEXT_3200_• multiplex.c.txt < prev    next >
Text File  |  2000-01-09  |  4KB  |  156 lines

  1. /* Multiplexing Daemon
  2.  *
  3.  * -Thanx to Ballbach, Iretd, Muffin.
  4.  *
  5.  *  This is a server that allows multiple connections at once.  It is not really "robust" but
  6.  *  more of a skeleton, so you can add on however you like.  What happens to the data 
  7.  *  is up to you.  It's probably easiest if you have the functions that mess with the data in
  8.  *  another file, that is what I did.  Compiles fine under linux.
  9.  *                                                                     -Seti
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h> 
  14. #include <errno.h> 
  15. #include <string.h> 
  16. #include <sys/time.h>
  17. #include <sys/types.h> 
  18. #include <netinet/in.h> 
  19. #include <sys/socket.h> 
  20. #include <sys/wait.h>
  21. #include <time.h>
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24.  
  25. /*  Some stuff to determine what to do with the data we get
  26.          #include "login_functions.c"
  27.          #include "decode.c"
  28. */
  29.  
  30. #define PORT 4019
  31. #define MAX_CONNECTIONS 15
  32.  
  33. char welcomeMessage[] = "Multiplexing Daemon\n";
  34. void setup(fd_set *, int *, int *, int);
  35. int free_slot(int *, int *);
  36.  
  37. main()
  38. {
  39. int sockfd, fds[MAX_CONNECTIONS], bytes, retval, freei, x=0, connections=0;
  40. int len = sizeof(struct sockaddr_in);
  41. struct sockaddr_in my_addr, their_addr;
  42. fd_set in;
  43. struct timeval timeout;
  44. char buf[50];
  45.  
  46.         timeout.tv_sec = 5;
  47.         timeout.tv_usec = 0;
  48.         
  49.         my_addr.sin_family = AF_INET;
  50.         my_addr.sin_port = htons(PORT);
  51.         my_addr.sin_addr.s_addr = INADDR_ANY;
  52.         bzero(&(my_addr.sin_zero), 8); 
  53.  
  54.         if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  55.             exit(1);
  56.  
  57.         if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))==-1) 
  58.             exit(1);
  59.  
  60.         if(listen(sockfd, MAX_CONNECTIONS) == -1)
  61.             exit(1);
  62.  
  63. while(1)
  64. {
  65.         setup(&in, &sockfd, fds, connections);
  66.         retval = select(MAX_CONNECTIONS, &in, NULL, NULL, &timeout);
  67.  
  68.  if(retval)
  69.   {
  70.  
  71.         if(FD_ISSET(sockfd, &in))
  72.          {
  73.                 printf("Accepting new connection...\n");
  74.                 freei = free_slot(&fds, &connections);
  75.  
  76.             if(!(freei == -1) && !(freei == MAX_CONNECTIONS))
  77.              {  
  78.                 printf("Creating socket fds[%d]: FD #:%d\n", freei, fds[freei]);
  79.                 fds[freei] = accept(sockfd, (struct sockaddr *)&their_addr, &len);
  80.                 FD_SET(fds[freei], &in);
  81.                 send(fds[freei], welcomeMessage, strlen(welcomeMessage), 0);
  82.              }
  83.          }
  84.   }
  85.  
  86. /* Check for incoming data and exceptions */
  87.  
  88. setup(&in, &sockfd, fds, connections);
  89. retval = select(MAX_CONNECTIONS, &in, NULL, NULL, &timeout);
  90.  
  91.  if(retval)
  92.   {
  93.         for(x=0; x<connections; x++)
  94.         {               
  95.                 if(FD_ISSET(fds[x], &in))
  96.                  {
  97.                         printf("Data Available\n");
  98.                         if((bytes = read(fds[x], buf, sizeof(buf))) <=0)
  99.                         {
  100.                                 close(fds[x]);
  101.                                 FD_CLR(fds[x], &in);
  102.                                 fds[x] = 0;
  103.                         }
  104.  
  105.                        else
  106.                         {
  107.                                 buf[bytes] = '\0';
  108.                                 printf("Data Received on fd[%d]: %s\n", x, buf);
  109.                                  //this is in decode.c
  110.                                 //decode(buf, fds[x]);
  111.                         }
  112.                  }       
  113.         }
  114.     }
  115.   }
  116. }
  117.  
  118.  
  119. /*** Initialize & Setup file descriptors
  120.  */
  121. void setup(fd_set *in, int *mainsock, int *fds, int i)
  122. {
  123. int x;
  124.  
  125.         FD_ZERO(in);
  126.         FD_SET((*mainsock), in);
  127.  
  128.         for(x=0; x<i; x++)
  129.                 FD_SET(fds[x], in);
  130. }
  131.  
  132.  
  133. /*** Return a free connection slot, if one is available
  134.  */
  135. int free_slot(int *fds, int *connections)
  136. {
  137. int x;
  138. int tmp[MAX_CONNECTIONS];
  139.  
  140.         for(x=0; x<(*connections); x++)
  141.          {
  142.                 if(fds[x] == 0)
  143.                  {
  144.                         fds[x] = tmp[x];  //fix it later
  145.                         return x;
  146.                  }
  147.          }
  148.                    
  149.  if((*connections) < MAX_CONNECTIONS)
  150.         return (*connections)++;
  151.  
  152.  if((*connections) == MAX_CONNECTIONS)
  153.         return (*connections);
  154.  
  155. return -1;
  156. }